package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.BarcodeField;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BarcodeFieldLineMapper;
import com.ruoyi.system.domain.BarcodeFieldLine;
import com.ruoyi.system.service.IBarcodeFieldLineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 条码字段行Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-26
 */
@Service
public class BarcodeFieldLineServiceImpl implements IBarcodeFieldLineService 
{
    @Autowired
    private BarcodeFieldLineMapper barcodeFieldLineMapper;

    /**
     * 查询条码字段行
     * 
     * @param id 条码字段行主键
     * @return 条码字段行
     */
    @Override
    public BarcodeFieldLine selectBarcodeFieldLineById(Long id)
    {
        return barcodeFieldLineMapper.selectBarcodeFieldLineById(id);
    }

    /**
     * 查询条码字段行列表
     *
     * @param hid 条码字段行头表主键
     * @return 条码字段行集合
     */
    @Override
    public List<BarcodeFieldLine> selectBarcodeFieldLineByHeaderId(long hid)
    {
        return barcodeFieldLineMapper.selectBarcodeFieldLineByHeaderId(hid);
    }

    /**
     * 查询条码字段行列表
     * 
     * @param barcodeFieldLine 条码字段行
     * @return 条码字段行
     */
    @Override
    public List<BarcodeFieldLine> selectBarcodeFieldLineList(BarcodeFieldLine barcodeFieldLine)
    {
        return barcodeFieldLineMapper.selectBarcodeFieldLineList(barcodeFieldLine);
    }

    /**
     * 新增条码字段行
     * 
     * @param barcodeFieldLine 条码字段行
     * @return 结果
     */
    @Override
    public int insertBarcodeFieldLine(BarcodeFieldLine barcodeFieldLine)
    {
        return barcodeFieldLineMapper.insertBarcodeFieldLine(barcodeFieldLine);
    }

    /**
     * 修改条码字段行
     * 
     * @param barcodeFieldLine 条码字段行
     * @return 结果
     */
    @Override
    public int updateBarcodeFieldLine(BarcodeFieldLine barcodeFieldLine)
    {
        return barcodeFieldLineMapper.updateBarcodeFieldLine(barcodeFieldLine);
    }

    /**
     * 批量删除条码字段行
     * 
     * @param ids 需要删除的条码字段行主键
     * @return 结果
     */
    @Override
    public int deleteBarcodeFieldLineByIds(Long[] ids)
    {
        return barcodeFieldLineMapper.deleteBarcodeFieldLineByIds(ids);
    }

    /**
     * 删除条码字段行信息
     * 
     * @param id 条码字段行主键
     * @return 结果
     */
    @Override
    public int deleteBarcodeFieldLineById(Long id)
    {
        return barcodeFieldLineMapper.deleteBarcodeFieldLineById(id);
    }

    /**
     * 保存条码字段行信息
     *
     * @param barcodeField 条码字段信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveBarcodeFieldLine(BarcodeField barcodeField) {
        List<BarcodeFieldLine> lineList = barcodeField.getLineList();
        List<BarcodeFieldLine> originFieldLines = barcodeFieldLineMapper.selectBarcodeFieldLineByHeaderId(barcodeField.getId());
        List<Long> x1 = lineList.stream().map(BarcodeFieldLine::getId).collect(Collectors.toList());
        List<Long> x2 = originFieldLines.stream().map(BarcodeFieldLine::getId).collect(Collectors.toList());

        // 计算 x2 - x1 的差集
        List<Long> toDeletes = x2.stream()
                .filter(id -> !x1.contains(id)) // 过滤掉 x1 中存在的元素
                .collect(Collectors.toList());

        for (BarcodeFieldLine line : lineList) {
            if (line.getId() == null ) {
                line.setHeaderId(barcodeField.getId());
                line.setCreatedBy(barcodeField.getCreateBy());
                barcodeFieldLineMapper.insertBarcodeFieldLine(line);
            }else{
                line.setModifiedBy(barcodeField.getUpdateBy());
                line.setModifiedDate(new Date());
                barcodeFieldLineMapper.updateBarcodeFieldLine(line);
            }
        }

        for (Long id : toDeletes) {
            barcodeFieldLineMapper.deleteBarcodeFieldLineById(id);
        }
        return 1;
    }
}
